#include "config.h"

std::tuple<std::vector <signal_info>, std::vector <trend_info>, std::vector <strategy_info>> get_execute_config(const char* cfg_file,uint32_t date)
{
	std::vector <strategy_info> strategy;
	std::vector <signal_info> signal;
	std::vector <trend_info> trend;
	pugi::xml_document doc;
	doc.load_file(cfg_file);
	pugi::xml_node root = doc.first_child();
	for (pugi::xml_node c = root.first_child(); c; c = c.next_sibling())
	{
		if(c.name()==std::string("execute"))
		{
			uint32_t begin = c.attribute("begin").as_uint();
			uint32_t end = c.attribute("end").as_uint();
			if (begin <= date && date <= end)
			{
				pugi::xml_node sig_node = c.child("signals");
				for (pugi::xml_node sn = sig_node.first_child(); sn; sn = sn.next_sibling())
				{
					signal_info info;
					info.id = sn.attribute("id").as_int();
					info.type = static_cast<signal_type>(sn.attribute("type").as_int());
					info.param = sn.attribute("param").as_string();
					signal.emplace_back(info);
				}
				pugi::xml_node trd_node = c.child("trends");
				for (pugi::xml_node sn = trd_node.first_child(); sn; sn = sn.next_sibling())
				{
					trend_info info;
					info.id = sn.attribute("id").as_int();
					info.type = static_cast<trend_type>(sn.attribute("type").as_int());
					info.param = sn.attribute("param").as_string();
					trend.emplace_back(info);
				}

				pugi::xml_node stra_node = c.child("strategys");
				for (pugi::xml_node sn = stra_node.first_child(); sn; sn = sn.next_sibling())
				{
					strategy_info info;
					info.id = sn.attribute("id").as_int();
					info.type = static_cast<strategy_type>(sn.attribute("type").as_int());
					info.openable = sn.attribute("openable").as_bool();
					info.closeable = sn.attribute("closeable").as_bool();
					info.param = sn.attribute("param").as_string();
					strategy.emplace_back(info);
				}
			}
		}
		
	}
	return std::make_tuple(signal, trend, strategy);
}

std::vector<uint32_t> get_trading_day_config(const char* cfg_file)
{
	std::vector<uint32_t> trading_days;
	pugi::xml_document trading_day_doc;
	trading_day_doc.load_file(cfg_file);
	auto trading_day_root = trading_day_doc.first_child();
	for (pugi::xml_node c = trading_day_root.first_child(); c; c = c.next_sibling())
	{
		uint32_t trading_day = std::atoi(c.first_child().value());
		trading_days.emplace_back(trading_day);
	}
	return trading_days;
}

